import {  FilesEvent } from "miniprogram/@types/Event"
import { File } from "miniprogram/miniprogram_npm/@vant/weapp/uploader/utils"
import { Form } from "../@types/form"
import {login, uploadfile} from "@utils/http"

// pages/verify/verify.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstForm:{} as Form,
    fileList: [] as File[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(option:{firstForm:string}) {
    this.setData({
      firstForm:JSON.parse(option.firstForm)
    })
  },
  delete(event:{detail:{index:string}}){
    this.setData({
      fileList:this.data.fileList.filter((_,index)=>index!=Number(event.detail.index))
    })
 },
  afterRead(e:FilesEvent) {
    var files = e.detail.file;
    this.setData({
       fileList:[...this.data.fileList,...files]
     })
  },
  async submit(){
    var that=this
   wx.showModal({
     title: "确定提交吗",
     success:async function(res){
       if(res.confirm){
        const {
          fileList,
          firstForm
        }=that.data
        const subject=firstForm.sub
          await  uploadfile('/user/auth',{
            file:fileList,
            ...firstForm,
            subject,
          },"file")
          await login()
          wx.switchTab({
            url:"/pages/home/home"
          })
       }
     }
   })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})